package org.zt.ccty.springboot_mybatis_demo.service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zt.ccty.springboot_mybatis_demo.config.mongodb.MongoDBConnFactory;
import org.zt.ccty.springboot_mybatis_demo.dao.UserDAO;
import org.zt.ccty.springboot_mybatis_demo.model.User;

import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

/**
 * Created by zl on 2015/8/27.
 */

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDAO userDao;
    @Autowired
    private MongoDBConnFactory mongoFactory;
    public List<User> getUserInfo(){
    	List<User> users=userDao.getUserInfo();
        //User user=null;
        return users;
    }

	public User getUserInfoById(String id) {
		User user = userDao.getUserInfoById(id);
		return user;
	}

	@Override
	public String testMongoDB() {
		String cityname = "beijing";
		//MongoDatabase database = new MongoDBConnFactory().getDatabase("credit_activity");
		MongoDatabase database = mongoFactory.getDatabase("credit_activity");
		MongoCollection<Document> collection = database.getCollection("cdt_city");
		//BasicDBObject filter = new BasicDBObject("city_code","7558");
		Map<String,Object> map = new HashMap<>();
		map.put("city_code","7558");
		BasicDBObject filter = new BasicDBObject(map);
		FindIterable<Document> result = collection.find(filter);
		//FindIterable<Document> result = collection.find();
		Document dc = result.first();
		if(dc != null){
			cityname = dc.getString("city_name");
		}
		return cityname;
	}

}
